package com.cskaoyan.util;

import java.time.Period;
import java.util.Calendar;
import java.util.Date;


/**
 * swn
 */
public class DateUtils {

    /**
     * 得到几天前的时间
     */

    public static Date getDateBefore(Date d, int day) {
        Calendar now = Calendar.getInstance();

        now.setTime(d);

        now.set(Calendar.DATE, now.get(Calendar.DATE) - day);

        return now.getTime();

    }

    /**
     * 得到几天后的时间
     */

    public static Date getDateAfter(Date d, int day) {
        Calendar now = Calendar.getInstance();

        now.setTime(d);

        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);

        return now.getTime();

    }

    /**
     * 计算days
     */

    public static Integer getDays(Date before, Date after) {

        long beforeTime = before.getTime();
        long afterTime = after.getTime();

        long period = afterTime - beforeTime;
        Integer days = (int) period / (24 * 60 * 60 * 1000);

        return days;
    }

}


